package com.example.unisys.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.Notice;
import com.example.unisys.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/apis/v1/notices", produces = "application/json; charset=utf-8")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    /*
     * 获取通知请求
     *
     * */
    @RequestMapping(value = "", method = {RequestMethod.GET})
    public Response getNotices(@RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("main_school") String main_school, @RequestParam("sub_school") String sub_school) {

        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        List<Map<String, Object>> ls = noticeService.getNoticeBySchool(main_school, sub_school, newlimit, pagesize);
        int total = noticeService.getNoticeSum(main_school, sub_school);
        return Response.builder().status(STATUS.SUCCESS).data(ls).total(total).build();
    }

    /*
     * 新建通知
     * */
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public Response createNotice(@RequestBody JSONObject jsonParam) {
        Notice notice = JSONObject.parseObject(jsonParam.toJSONString(), Notice.class);
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String time = df.format(new Date());
        // System.out.println("title is "+notice.getnotice_title());
//        notice.setPub_date(time);
        if (noticeService.insert(notice) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
     * 删除通知
     * */
    @RequestMapping(value = "/{notice_id}", method = {RequestMethod.DELETE})
    public Response removeById(@PathVariable("notice_id") int notice_id) {
        if (noticeService.delete(notice_id) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
     * 获取通知by id
     * */
    @RequestMapping(value = "/{notice_id}", method = {RequestMethod.GET})
    public Response getById(@PathVariable("notice_id") int notice_id) {
        Notice notice = noticeService.getById(notice_id);
        return Response.builder().data(notice).status(STATUS.SUCCESS).build();
    }

    /*
     * 更改通知
     * */
    @RequestMapping(value = "/{notice_id}", method = {RequestMethod.PUT})
    public Response modifyNoticeById(@RequestBody JSONObject jsonParam) {
        Notice notice = JSONObject.parseObject(jsonParam.toJSONString(), Notice.class);
        if (noticeService.update(notice) == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }


}
